package le.ralph.quwen.cn.java.view.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import le.ralph.quwen.cn.R;
import le.ralph.quwen.cn.java.utils.VerifyUtil;

public class RegisterActivity extends AppCompatActivity {

    private TextView tvLogin;
    private ImageView ivClose;
    private TextView tvZone;
    private EditText etPhone;
    private EditText etVerifyCode;
    private TextView tvVerifyCode;
    private EditText etPassword;
    private EditText etPasswordVerify;
    private Button btnRegister;
    private boolean isVerifyCodeGetting;
    private MyCountDownTimer myCountDownTimer;
    private EditText etUsername;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
        initListener();
    }

    private void initView() {
        btnRegister = (Button) findViewById(R.id.btn_register);
        etPasswordVerify = (EditText) findViewById(R.id.et_register_password_verify);
        etPassword = (EditText) findViewById(R.id.et_register_password);
        tvVerifyCode = (TextView) findViewById(R.id.tv_verify_code_count);
        etVerifyCode = (EditText) findViewById(R.id.et_register_verify_code);
        etPhone = (EditText) findViewById(R.id.et_register_phone);
        etUsername = (EditText) findViewById(R.id.et_register_username);
        tvZone = (TextView) findViewById(R.id.tv_register_zone);
        ivClose = (ImageView) findViewById(R.id.iv_register_close);
        tvLogin = (TextView) findViewById(R.id.tv_login);
        tvVerifyCode.setClickable(false);
        tvVerifyCode.setTextColor(Color.GRAY);
        btnRegister.setClickable(false);
        btnRegister.setBackgroundResource(R.drawable.ic_check_circle_grey);
    }

    private void initListener() {
        tvLogin.setOnClickListener(defaultOnClickListener);
        tvVerifyCode.setOnClickListener(defaultOnClickListener);
        btnRegister.setOnClickListener(defaultOnClickListener);
        ivClose.setOnClickListener(defaultOnClickListener);
        etPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (isVerifyCodeGetting) {
                    tvVerifyCode.setClickable(false);
                    tvVerifyCode.setTextColor(Color.GRAY);
                } else {
                    if (s.length() == 0) {
                        tvVerifyCode.setClickable(false);
                        tvVerifyCode.setTextColor(Color.GRAY);
                    } else {
                        tvVerifyCode.setClickable(true);
                        tvVerifyCode.setTextColor(Color.WHITE);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        etPasswordVerify.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 0) {
                    btnRegister.setClickable(false);
                    btnRegister.setBackgroundResource(R.drawable.ic_check_circle_grey);
                } else {
                    if (s.equals(etPassword.getText().toString())) {
                        btnRegister.setClickable(true);
                        btnRegister.setBackgroundResource(R.drawable.ic_check_circle_white);
                    } else {
                        btnRegister.setClickable(false);
                        btnRegister.setBackgroundResource(R.drawable.ic_check_circle_grey);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private View.OnClickListener defaultOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            switch (id) {
                case R.id.tv_login:
                    Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                    startActivity(intent);
                    break;
                case R.id.tv_verify_code_count:
                    getVerifyCode();
                    break;
                case R.id.btn_register:
                    break;
                case R.id.iv_register_close:
                    break;
            }
        }
    };

    private void getVerifyCode() {
        String phone = etPhone.getText().toString().trim();
        if (VerifyUtil.isMobile(phone)) {
            //发送验证码
            isVerifyCodeGetting = true;
            checkInfos();
            startCount();
        } else {
            showToast("请输入正确的手机号码");
        }
    }

    private void checkInfos() {
        String phone = etPhone.getText().toString().trim();
        String userName = etUsername.getText().toString();
        String password = etPassword.getText().toString();
        String passwordVerify = etPasswordVerify.getText().toString();
        String verifyCode = etVerifyCode.getText().toString();
        if (TextUtils.isEmpty(verifyCode)) {
            showToast("请输入收到的验证码");
        }
        if (TextUtils.isEmpty(userName)) {
            showToast("请输入昵称");
        }
        if (TextUtils.isEmpty(password)) {
            showToast("请输入正确的密码");
        } else if (!TextUtils.equals(password, passwordVerify)) {
            showToast("两次密码不一致，请确认密码");
        }
        if (!TextUtils.isEmpty(verifyCode) && !TextUtils.isEmpty(userName) &&
                !TextUtils.isEmpty(password) && !TextUtils.isEmpty(passwordVerify)) {

        }

    }

    private void startCount() {
        if (myCountDownTimer == null) {
            myCountDownTimer = new MyCountDownTimer(90000, 1000);
        }
        myCountDownTimer.start();
    }

    //倒计时函数
    private class MyCountDownTimer extends CountDownTimer {

        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        //计时过程
        @Override
        public void onTick(long l) {
            //防止计时过程中重复点击
            tvVerifyCode.setClickable(false);
            tvVerifyCode.setText(l / 1000 + "秒");
        }

        //计时完毕的方法
        @Override
        public void onFinish() {
            //重新给Button设置文字
            tvVerifyCode.setText("重新获取");
            //设置可点击
            tvVerifyCode.setClickable(true);
            clearTimer();
        }
    }

    private void clearTimer() {
        if (myCountDownTimer != null) {
            myCountDownTimer.cancel();
            myCountDownTimer = null;
        }
    }

    private Toast toast;

    private void showToast(String content) {
        if (toast == null) {
            toast = Toast.makeText(RegisterActivity.this, content, Toast.LENGTH_SHORT);
        }
        toast.setText(content);
        toast.show();
    }
}
